<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--360浏览器优先以webkit内核解析-->
    <title> 添加文章</title>
    <link rel="shortcut icon" href="assets/manager/favicon.ico"> 
    <link href="assets/manager/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="assets/manager/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="assets/manager/css/animate.css" rel="stylesheet">
    <link href="assets/manager/css/style.css?v=4.1.0" rel="stylesheet">
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="addarticle">
            <div class="row">
                <div class="col-sm-3">
                    <span class="userMbq">标签：</span>
                    <select id="tags" class="form-control m-b userMsleinp" name="account"></select>
                </div>
                <div class="col-sm-3">
                    <div class="form-group" style="line-height: 30px;">
                        	原创/转载：
                        <label class="radio-inline">
                        <input type="radio" checked value="原创" id="yuanc" name="type">原创</label>
                        <label class="radio-inline">
                        <input type="radio" value="转载" id="zhuanz" name="type">转载</label>
                    </div>
                </div>
            </div>
            <div class="form-group addform-group">
                <label class="col-sm-1 control-label">标题</label>
                <div class="col-sm-11">
                    <input type="text" id="title" class="form-control" required>
                </div>
            </div>
            <div class="form-group addform-group">
                <label class="col-sm-1 control-label">简介</label>
                <div class="col-sm-11">
                    <textarea id="intro" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group addform-group">
                <label class="col-sm-1 control-label">内容</label>
                <div class="col-sm-11">
                    <script id="editor" type="text/plain" style="width:100%;height:500px;">初始化的内容。</script>
                </div>
            </div>
            <div class="form-group addform-group">
                <button type="button" id="save" class="btn btn-outline btn-info">保存</button>
                <button type="button" id="close" class="btn btn-outline btn-warning">取消</button>
            </div>
        </div>
    </div>
    <!-- 全局js -->
    <script src="assets/manager/js/jquery.min.js?v=2.1.4"></script>
    <script src="assets/manager/js/bootstrap.min.js?v=3.3.6"></script>
    <script src="assets/manager/js/plugins/layer/layer.min.js"></script>
    <!-- Flot -->
    <script src="assets/manager/js/plugins/flot/jquery.flot.js"></script>
    <script src="assets/manager/js/plugins/flot/jquery.flot.tooltip.min.js"></script>
    <script src="assets/manager/js/plugins/flot/jquery.flot.resize.js"></script>
    <script src="assets/manager/js/plugins/flot/jquery.flot.pie.js"></script>
    <!-- 自定义js -->
    <script src="assets/manager/js/content.js"></script>
    <!-- 配置文件 -->
	<script type="text/javascript" charset="UTF-8" src="./ueditor/ueditor.config.js"></script>
	<!-- 编辑器源码文件 -->
	<script type="text/javascript" charset="utf-8" src="./ueditor/ueditor.all.min.js"> </script>
	<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
	<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
	<script type="text/javascript" charset="utf-8" src="./ueditor/lang/zh-cn/zh-cn.js"></script>
    <!--flotdemo-->
    <script type="text/javascript">
	  	//实例化编辑器
	    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
	    var ue = UE.getEditor('editor',{
	    	//这里可以选择自己需要的工具按钮名称,此处仅选择如下五个
	        toolbars:[[
	                   'fullscreen', 'source', '|', 'undo', 'redo', '|',
	                   'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|',
	                   'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
	                   'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
	                   'directionalityltr', 'directionalityrtl', 'indent', '|',
	                   'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
	                   'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
	                   'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|',
	                   'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|',
	                   'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|',
	                   'print', 'preview', 'searchreplace', 'drafts', 'help'
	               ]],
	        //focus时自动清空初始化时的内容
	        autoClearinitialContent:true,
	        //关闭字数统计
	        wordCount:false,
	        //关闭elementPath
	        elementPathEnabled:false,
	        //默认的编辑区域高度
	        initialFrameHeight:300
	        //更多其他参数，请参考ueditor.config.js中的配置项
	    });
	    ue.ready(function(){
	    	$.get("getTags",function(data){
		    	$tagsBox = $("#tags");
		    	var $firstOption = $("<option>请选择</option>");
		    	$tagsBox.append($firstOption);
				for(var i=0;i<data.length;i++){
					var $option = $("<option>" + data[i] + "</option>");
					$tagsBox.append($option);
				}
				getArticle();
			});
	    });
    	
	    $("#save").click(function(){
	    	var $tagsVal = $("#tags").val();
	    	if($tagsVal=="请选择"){
	    		alert("请选择标签类型！");
	    	}else if($("#title").val() == "" || $("#intro").val() == "" || $("#content").val() == ""){
	    		alert("必须填写所有内容！");
	    	}else{
	    		upDateArticle();
	    	}
	    });
	    
	    function getArticle(){
	    	$.get("getArticleById",function(data){
	    		$("#tags").val(data.tag);
	    		if($("#yuanc").val() == data.type){
	    			$("#yuanc").attr("checked",true);
	    		}else{
	    			$("#zhuanz").attr("checked",true);
	    		}
	    		$("#title").val(data.title);
	    		$("#intro").val(data.intro);
	    		ue.setContent(data.content);
	    	});
	    }
	    
	    function upDateArticle(){
	    	var $type = $(".form-group input[type='radio']:checked").val();
	    	var $content = ue.getContent();
	    	var $dataObject = {
	    			tag:$("#tags").val(),
	    			type:$type,
	    			title:$("#title").val(),
	    			intro:$("#intro").val(),
	    			content:$content
	    	}
	    	$.ajax({
				type:"post",
				url:"upDateArticle",
				dataType:"json",
				contentType:"application/json;charset=utf-8",
				data:JSON.stringify($dataObject),
				success:function(data){
					if(data == 1){
						alert("修改成功！");
						window.location.href="myarticle";
					}else{
						alert(data.resMsg);
					}
				}
			})
	    }
	    
	    $("#close").click(function(){
			window.location.href="myarticle";
		});
    </script>
</body>
</html>
